package com.action;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.entity.UserInfo;
import com.form.UserForm;

public class UserAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		UserForm userForm = (UserForm) form;

		String id = userForm.getId();
		String pw = userForm.getPassword();
		String mailaddress = userForm.getEmailaddress();
		String kokyakumeikanji = userForm.getNameKanji();
		String kokyakumeikana = userForm.getNameKana();
		String nyuusya = userForm.getHiredYYYYMM();
		String yakusyoku = userForm.getPosition();

		Connection conn = null;

		String url = "jdbc:mysql://localhost:3306/shangpin";
		String user = "root";
		String password = "rosy";

		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();

			conn = DriverManager.getConnection(url, user, password);
			Statement stmt = conn.createStatement();

			// String sql =
			// "INSERT INTO userinfo"+"("+"ID,Password,emailaddress,NameKanji,NameKana,HiredYYYYMM,Position"+")"+" VALUES('"+id+"','"+pw+"','"+mailaddress+"','"+kokyakumeikanji+"','"+kokyakumeikana+"','"+nyuusya+"','"+yakusyoku+"'"+")";
			String sql = "INSERT INTO userinfo"
					+ "("
					+ "ID,Password,emailaddress,NameKanji,NameKana,HiredYYYYMM,Position"
					+ ")" + " VALUES('" + id + "','" + pw + "','" + mailaddress
					+ "','" + kokyakumeikanji + "','" + kokyakumeikana + "','"
					+ nyuusya + "','" + yakusyoku + "'" + ")";
			stmt.executeUpdate(sql);
			stmt.close();
			// pstmt.close();
			conn.close();
		} catch (SQLException e) {
			// TODO 自動生成された catch ブロック
			System.out.println("Sql exception");
		} catch (ClassNotFoundException e) {
			System.out.println("Class not found");

		} catch (Exception e) {
			System.out.println("exception");
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				response.getWriter().println(e.getMessage());
			}
		}
		return mapping.findForward("success");
	}

}
